/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package x10.event;

import java.io.Serializable;

import x10.enums.InterfaceMessage;
import x10.utilities.ChunkOfBytes;

/**
 *
 * @author Denny
 */
public class X10InterfaceRequest extends X10Event implements Serializable {
    static final long serialVersionUID = 1L;
    private ChunkOfBytes bytes = new ChunkOfBytes();
    InterfaceMessage message;
    byte addressHigh;
    byte addressLow;
    private String address;
    private String source;
    private long instantiatedMillis;

    public X10InterfaceRequest(InterfaceMessage message, byte addressHigh, byte addressLow, String source){
        this.instantiatedMillis = System.currentTimeMillis();
        this.address = "INTERFACE";
        this.message = message;
        this.addressHigh = addressHigh;
        this.addressLow = addressLow;
        this.source = source;
        configure();
    }

    private void configure(){
        if(message == InterfaceMessage.STATUS_REQUEST_0X8B){
            byte[] temp = new byte[1];
            temp[0] = (byte)0x8B;
            bytes.setBytes(temp);
            bytes.setLength(1);
            bytes.setEmpty(false);
            bytes.setInterfaceReceiveCycles(2);

        } else if (message == InterfaceMessage.SET_CLOCK_0X9B){

        } else if (message == InterfaceMessage.UNKNOWN_0XAB){

        } else if (message == InterfaceMessage.UNKNOWN_0XBB){

        } else if (message == InterfaceMessage.MEMORY_DUMP_0XDB){

        }
        bytes.setInterfaceRequest(true);
        bytes.setInterfaceMessage(message);

    }

    /**
     * @return the source
     */
    public String getSource() {
        return source;
    }

    /**
     * @param source the source to set
     */
    public void setSource(String source) {
        this.source = source;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the instantiatedMillis
     */
    public long getInstantiatedMillis() {
        return instantiatedMillis;
    }

    /**
     * @param instantiatedMillis the instantiatedMillis to set
     */
    public void setInstantiatedMillis(long instantiatedMillis) {
        this.instantiatedMillis = instantiatedMillis;
    }

    public ChunkOfBytes getBytes() {
        return bytes;
    }

    public void setBytes(ChunkOfBytes bytes) {
        this.bytes = bytes;
    }

    public boolean equals(X10Event device){
        boolean isEqual = true;

        if(device instanceof X10InterfaceRequest){
            X10InterfaceRequest other = (X10InterfaceRequest)device;
            if(other!=null){
                //If both objects are within 1 second of each other, call them equal
                long low = this.getInstantiatedMillis() - 1000; //1 second earlier
                long high = this.getInstantiatedMillis() + 1000; //1 second later
                long otherMillis = other.getInstantiatedMillis();

                if(!((otherMillis >= low) && (otherMillis <= high))){
                    isEqual = false;
                }
                if(!this.source.contentEquals(other.getSource())){
                    isEqual = false;
                }
            }
        } else {
            isEqual = false;
        }

        return isEqual;
    }

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("INTERFACE REQUEST Source: ");
        sb.append(source);
        sb.append(" Type: ");
        sb.append(message.toString());
        sb.append(" Transmitted:");
        sb.append(bytes.toString());
        return sb.toString();
    }


}
